<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>分配权限窗口</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/static/layui/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/static/layui/css/public.css" media="all" />
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/static/zTree_v3/css/bootstrapStyle/bootstrapStyle.css">
    <script type="text/javascript" src="<%=request.getContextPath()%>/static/zTree_v3/js/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/static/zTree_v3/js/jquery.ztree.all.min.js"></script>
</head>
<body>
    <div>
        <input type="hidden" id="roleIdHiden"/>
        <ul id="regionZTree" class="ztree"></ul>
        <button class="layui-btn" onclick="setPrivilege()">修改权限</button>
    </div>
</body>
</html>
<script>
    $(function () {
        var setting = {
            view: {
                dblClickExpand: false,//双击节点时，是否自动展开父节点的标识
                showLine: true,//是否显示节点之间的连线
                fontCss:{'color':'black'},//字体样式函数
                selectedMulti: false //设置是否允许同时选中多个节点
            },
            check:{
                chkboxType: { "Y": "", "N": "" },
                chkStyle: "checkbox",//复选框类型
                enable: true //每个节点上是否显示 CheckBox
            },
            data: {
                simpleData: {//简单数据模式
                    enable:true,
                    idKey: "id",
                    pIdKey: "pid",
                    rootPId: null
                }
            },
            callback: {
                //onClick:zTreeBeforeExpand // 用于捕获父节点展开之前的事件回调函数，并且根据返回值确定是否允许展开操作
            }
        };
        $.ajax({
            url:"<%=request.getContextPath()%>/sys/perMenus.html",
            type:"get",
            dataType:"json",
            success:function(data){
                zNodes=data;
                zTreeObj = $.fn.zTree.init($("#regionZTree"), setting, data);//初始化操作
                zTreeObj.expandAll(true);
                //获得已有权限，绑定到树上
                getChecked();
            }
        });
    });
    //从后台获取本角色有的权限
    function getChecked() {
        var rid=document.getElementById("roleIdHiden").value;
        console.log(rid);
        $.ajax({
            url:'<%=request.getContextPath()%>/sys/getCheckedMenu.html?rid='+rid,
            type:"get",
            dataType:"json",
            success:function(data){
                var result=data;
                for(var i=0;i<result.length;i++){
                    zTree=$.fn.zTree.getZTreeObj("regionZTree");
                    var node=zTree.getNodeByParam("id",result[i]);
                    if(node!=null){
                        zTree.checkNode(node,true);
                    }
                }
            }
        });
    }
    //修改权限
    function setPrivilege(){
        zTree=$.fn.zTree.getZTreeObj("regionZTree");
        if(zTree!=null){
            //获取被勾选的节点集合，true表示被勾选
            var nodes=zTree.getCheckedNodes(true);
            var str='';
            $(nodes).each(function () {
                str+=this.id+",";
            });
            var rid=document.getElementById("roleIdHiden").value;
            $.ajax({
                url:'<%=request.getContextPath()%>/sys/doAssignPermission.html',
                type:"POST",
                data:{"rid":rid,"menuIdsStr":str},
                dataType:"json",
                success:function (data) {
                    if(data.code==200){
                        window.alert("分配成功!");
                        zTree.refresh();//刷新树

                    }
                }
            });
        }
    }
</script>
